package org.redemptionhost.web.panels;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.redemptionhost.Repository;
import org.redemptionhost.model.Host;
import org.redemptionhost.model.HostAge;
import org.redemptionhost.web.pages.AdminPage;
import org.redemptionhost.web.pages.Guestbook;

import com.google.inject.Inject;

public class AddHostPanel extends Panel implements Serializable {
	private static final long serialVersionUID = 1L;
	
    @Inject
    private Repository<Host> hostRepository;

	public AddHostPanel(String id, final Boolean isAdministration) {
		super(id);

        Form<Void> addHostForm = new Form<Void>("form") {
        	public String selectedAbbreviation = "  ";
        	public String selectedAge = " ";
        	
            private RequiredTextField<String> firstNameField;
            {
            	firstNameField = new RequiredTextField<String>("firstName", new Model<String>(""));
            	add(firstNameField);
            }

            private RequiredTextField<String> lastNameField;
            {
            	lastNameField = new RequiredTextField<String>("lastName", new Model<String>(""));
            	add(lastNameField);
            }

            private DropDownChoice<String> ageField;
            {
            	List<String> ageList = Arrays.asList(new String[] {
            			" ", "15", "16-17", "18 or older"
            	});
            	ageField = new DropDownChoice<String>("ageDDC", 
            			new PropertyModel<String>(this, "selectedAge"), ageList);
            	add(ageField);
            }

            private RequiredTextField<String> streetAddressField;
            {
            	streetAddressField = new RequiredTextField<String>("streetAddress", new Model<String>(""));
            	add(streetAddressField);
            }
           
            private RequiredTextField<String> cityField;
            {
            	cityField = new RequiredTextField<String>("city", new Model<String>(""));
            	add(cityField);
            }

            private DropDownChoice<String> stateField;
            {
            	List abbreviations = Arrays.asList(new String[] {
            			"  ", "AB", "AK", "AL", "AR", "AZ", "BC", "CA", "CO", "CT", 
            			"DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS",
            			"KY", "LA", "MA", "MB", "MD", "ME", "MI", "MN", "MO", "MS",
            			"MT", "NB", "NC", "ND", "NE", "NH", "NJ", "NL", "NM", "NS",		
            			"NT", "NU", "NV", "NY", "OH", "OK", "ON", "OR", "PA", "PE",		
            			"QC", "RI", "SC", "SD", "SK", "TN", "TX", "UT", "VA", "VT",
            			"WA", "WI", "WV", "WY", "YT" });
            	stateField = new DropDownChoice<String>("stateDDC", 
            			new PropertyModel(this, "selectedAbbreviation"), abbreviations);
            	add(stateField);
            }
            
           private RequiredTextField<String> zipField;
           {
        	   zipField = new RequiredTextField<String>("zip", new Model<String>(""));
        	   add(zipField);
           }
            
            private RequiredTextField<String> phoneField;
            {
            	phoneField = new RequiredTextField<String>("phone", new Model<String>(""));
            	add(phoneField);
            }
            
            private RequiredTextField<String> emailField;
            {
            	emailField = new RequiredTextField<String>("email", new Model<String>(""));
            	add(emailField);
            }

            Label label = new Label("administratorLabel", "Is an Administrator:");
            {
            	label.setVisible(isAdministration);
            	label.setEnabled(isAdministration);
            	add(label);
            }
            
            private CheckBox administratorField = new CheckBox("isAdministrator", new Model<Boolean>(false));
            {
            	administratorField.setVisible(isAdministration);
            	administratorField.setEnabled(isAdministration);
            	add(administratorField);
            }
                     
            @Override
            protected void onSubmit()
            {
                String firstName = firstNameField.getModelObject();
                String lastName = lastNameField.getModelObject();
                String ageString = ageField.getModelObject();
                HostAge age = (ageString.equalsIgnoreCase("15")) ?
                		HostAge.FIFTEEN : (ageString.equals("16-17")) ?
                		HostAge.SIXTEENSEVENTEEN : HostAge.EIGHTEENPLUS;                
                String streetAddress = streetAddressField.getModelObject();
                String city = cityField.getModelObject();
                String state = stateField.getModelObject();
                String zip = zipField.getModelObject();
                String phone = phoneField.getModelObject();
                String email = emailField.getModelObject();
                Boolean administrator = administratorField.getModelObject();
            	
                Host newHost = new Host(
                			firstName,
                			lastName,
                			age,
                			streetAddress,
                			city,
                			state,
                			zip,
                			phone,
                			email,
                			UUID.randomUUID().toString(),
                			administrator
                        );
                
                AddHostPanel.this.hostRepository.persist(newHost);
                
                // This causes a redirect to a clean page and URL, rather than rendering to the state of the
                // current page which we don't care about here.
                setResponsePage(AdminPage.class);
            }
  
        };
        add(addHostForm);
	}
}
